package com.example.database;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button btn_login,btn_enroll_a;
    CheckBox cb_autologin;
    EditText et_username,et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_enroll_a=(Button)findViewById(R.id.btn_enroll_a);
        et_username=(EditText) findViewById(R.id.et_username);
        et_password=(EditText) findViewById(R.id.et_password);
        cb_autologin=(CheckBox) findViewById(R.id.cb_autologin);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Database helper=new Database(MainActivity.this);
                String password = et_password.getText().toString();
                String username = et_username.getText().toString();
                if (!password.isEmpty()&!username.isEmpty()) {
                    User users = helper.query(username);
                    String password_1 = users.getPassword();
                    String username_1 = users.getUsername();
                    if (!password_1.isEmpty() & !username_1.isEmpty()) {
                        if (password.equals(password_1) & username.equals(username_1)) {
                            if (cb_autologin.isChecked()) {
                                SharedPreferences sp = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
                                SharedPreferences.Editor edt = sp.edit();
                                edt.putString("user", et_username.getText().toString());
                                edt.putString("password", et_password.getText().toString());
                                edt.commit();
                            } else {
                                SharedPreferences sp = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
                                SharedPreferences.Editor edt = sp.edit();
                                edt.putString("user", null);
                                edt.putString("password", null);
                                edt.commit();
                            }
                            Toast toast = Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT);
                            toast.show();
                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this, Calculate.class);
                            startActivity(intent);
                            finish();
                        } else if (!password.equals(password_1) & username.equals(username_1)) {
                            Toast toast = Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT);
                            toast.show();
                        } else if (password.equals(password_1) & !username.equals(username_1)) {
                            Toast.makeText(MainActivity.this, "用户名错误", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "未查询到用户名，请您先注册", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }
            }

        });
        btn_enroll_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,EnrollActivity.class);
                startActivity(intent);
                finish();
            }
        });
        getdata();

    }
    private void getdata() {
        SharedPreferences sp=getSharedPreferences("PREFS_NAME",MODE_PRIVATE);
        et_username.setText(sp.getString("user",""));
        et_password.setText(sp.getString("password",""));
    }
}